package main

import (
	"io"
	"log"
	"net"
	"os"
)

func mustCopy(w io.Writer, r io.Reader) {
	buf := [64]byte{}
	for {
		n, err := r.Read(buf[:])
		if err != nil {
			log.Println(err)
			break
		}
		_, err = w.Write(buf[0:n])
		if err != nil {
			log.Println(err)
			break
		}
	}
}

func main() {
	co, err := net.Dial("tcp", "localhost:8001")
	if err != nil {
		log.Println(err)
		return
	}

	conn := co.(*net.TCPConn)

	done := make(chan struct{})
	go func() {
		mustCopy(os.Stdout, conn)
		log.Println("done")
		done <- struct{}{}
	}()

	mustCopy(conn, os.Stdin)
	conn.CloseWrite()
	<-done
	log.Println("exit")
}
